<template>
  <div id="kpi" class="box-body">
    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title">年度考核</h3>
        <div class="box-tools pull-right">
          <button type="button" v-if='isEditable' class="btn btn btn-success" @click="editVisiable = true">
              <i class='fa fa-plus-square-o'>年度考核</i>
            </button>
        </div>
      </div>
      <div class="box-body">
        <div class=" form-inline dt-bootstrap" id="example1_wrapper">
          <div class="row">
            <div class="col-sm-12 table-responsive">
              <table class="table table-hover table table-bordered table-striped dataTable">
                <tbody>
                  <tr>
                    <th aria-label="考核年度" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">考核年度</th>
                    <th aria-label="考核结果" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">考核结果</th>
                    <th v-if='isEditable' aria-label="操作" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">操作</th>
                  </tr>
                  <tr :key='index' v-for='(value,index) in this.kpis' role="row" class="value.cls">
                    <td>{{ value.year }}</td>
                    <td class="text-light" v-text="getresultname(value.result)"></td>
                    <td v-if='isEditable'>
                      <div class="col-md-2">
                        <button @click="deleteKPI(index)" class="btn btn-xs">删除</button>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      <el-dialog id='kpiDlg' title="新增年度考核" :visible.sync="editVisiable" :modal="false" size="small">
        <!-- <b-modal id='kpiDlg' title='新增年度考核' close-title='关闭' ok-title='保存' @ok='submit' @shown='clearEditData'> -->
        <el-form :model="editKPI" ref="editKPI" label-width="200px">
          <el-row>
            <div class="col-md-6">
              <el-form-item label="考核年度" :required="true">
                <el-select :disabled="!isEditable" v-model="editKPI.year">
                  <el-option v-for="item in this.years" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div class="col-md-6">
              <el-form-item label="考核结果" :required="true">
                <el-select :disabled="!isEditable" v-model="editKPI.result">
                  <el-option v-for="item in this.results" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editVisiable = false">关 闭</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import Utils from "../../../common/utils"
  let util = new Utils()
  export default {
    name: 'KPI',
    props: ['kpis', 'editable'],
    data: function() {
      return {
        isEditable: true,
        editVisiable: false,
        editKPI: {
          year: new Date().getFullYear(),
          result: 1
        },
        results: [],
        years: []
      }
    },
    created() {
      if (this.editable != null) {
        this.isEditable = this.editable
      }
      this.results = this.$store.state.sysconfigs.kpiresults
      this.years = util.getKpiYears()
    },
    methods: {
      clearEditData() {
        this.editKPI.year = 2017,
        this.editKPI.result = 1
      },
      getname(cates, id) {
        for (var index in cates) {
          if (cates[index].id == id) {
            return cates[index].name;
          }
        }
        return ''
      },
      getresultname(id) {
        return this.getname(this.results, id)
      },
      deleteKPI(index) {
        this.kpis.splice(index, 1)
      },
      deepCopy(o, c) {
        var c = c || {}
        for (var i in o) {
          if (typeof o[i] === 'object') {
            if (o[i].constructor === Array) {
              c[i] = []
            } else {
              c[i] = {}
            }
            this.deepCopy(o[i], c[i])
          } else {
            c[i] = o[i]
          }
        }
        return c
      },
      submit(e) {
        var kpi = {}
        this.deepCopy(this.editKPI, kpi)
        this.kpis.push(kpi)
        this.clearEditData()
        this.editVisiable = false
      }
    }
  }
</script>

<style scoped>
  #kpi {
    min-height: 400px;
  }
</style>